/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package session.stateful;

import entity.DepartmentEntity;
import entity.RightEntity;
import entity.SystemUserEntity;
import exception.EntityNotFoundException;
import java.util.Collection;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateful;
import javax.ejb.LocalBean;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import session.stateless.SystemUserSessionBean;

/**
 *
 * @author wei
 */
@Stateful
@LocalBean
public class DepartmentSessionBean {

    @PersistenceContext
    EntityManager em;
    DepartmentEntity department;
    @EJB
    SystemUserSessionBean employeeManager;
    
    public DepartmentEntity createOrUpdateDepartment(DepartmentEntity d){
        department = em.merge(d);
        em.flush();
        return department;
    }
    
    public DepartmentEntity findDepartmentById(Long id){
        return em.find(DepartmentEntity.class, id);
    }
    
    public List<DepartmentEntity> getDepartmentByName(String name) {
        Query query = em.createQuery(
                "SELECT d FROM Department d WHERE d.name = :inName");
        query.setParameter("inName", name);
        List<DepartmentEntity> departments = query.getResultList();

        return departments;
    }
    
    public void addEmployeeToDepartment(Long employeeId, Long departmentId) 
            throws EntityNotFoundException{
        SystemUserEntity employee = employeeManager.getSystemUserById(employeeId);
        if(employee == null)
            throw new EntityNotFoundException("Employee with id + " + employeeId + " is not found.");
        addEmployeeToDepartment(employee, departmentId);
    }
    
    public void addEmployeeToDepartment(SystemUserEntity employee, Long departmentId) 
            throws EntityNotFoundException{
        department = findDepartmentById(departmentId);
        if(department == null)
            throw new EntityNotFoundException("Department with id + " + departmentId + " is not found.");
        Collection<SystemUserEntity> employees = department.getEmployees();
        employees.add(employee);
        createOrUpdateDepartment(department);
    }
    
    public void removeEmployeeFromDepartment(Long employeeId, Long departmentId) 
            throws EntityNotFoundException{
        SystemUserEntity employee = employeeManager.getSystemUserById(employeeId);
        if(employee == null)
            throw new EntityNotFoundException("Employee with id + " + employeeId + " is not found.");
        removeEmployeeFromDepartment(employee, departmentId);
    }

    public void removeEmployeeFromDepartment(SystemUserEntity employee, Long departmentId) 
            throws EntityNotFoundException {
        department = findDepartmentById(departmentId);
        if(department == null)
            throw new EntityNotFoundException("Department with id + " + departmentId + " is not found.");
        employee.setDepartment(department);
        employeeManager.createOrUpdateSystemUser(employee);
    }
    
    public List<DepartmentEntity> listAllDepartments(){
        Query query = em.createQuery("SELECT d FROM Department d");
        return query.getResultList();
    }
    

}
